<template>
	<view class="gold_modify">
		<view class="gold_info">
			<view class="gold_name">黄金价格</view>
			<view class="gold_time">更新时间:{{currentTime}}</view>
		</view>
		<view class="gold_content">
			<view class="gold_content_info">
				<view class="gold_content_name">今日价格</view>
				<view class="gold_content_price">
					<input type="digit" v-model="glod_price.askprice"/>
					<view class="gold_content_dw">元/克</view>
				</view>
			</view>
			<view class="gold_content_info">
				<view class="gold_content_name">回收价格</view>
				<view class="gold_content_price">
					<input type="digit" v-model="glod_price.bidprice" />
					<view class="gold_content_dw">元/克</view>
				</view>
			</view>
		</view>
		<view class="gold_modify_btn" @tap="submit_envent">提交</view>
	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
				currentTime: '',
				glod_price: {},
			}
		},
		onLoad(params) {
			if (typeof(params) != 'undefined') {
				var numberId = params.numberId || '';
				if (numberId != '') {
					uni.clearStorageSync();
					app.globalData.data.numberId = numberId;
				}
			}
			this.init();
		},
		onShow() {
			this.entryTime = Date.now();
		},
		onUnload() {
			this.OnEntryTime();
		},
		methods: {
			init(e) {
				var user = app.globalData.get_user_info(this, "init", "");
				if (user != false) {
					this.getglod_price();
					let timer = setInterval(() => {
						let currentTime = new Date();

						let year = currentTime.getFullYear();

						let month = currentTime.getMonth() + 1;

						let date = currentTime.getDate();

						let hours = this.addZero(currentTime.getHours());

						let minutes = this.addZero(currentTime.getMinutes());

						let seconds = this.addZero(currentTime.getSeconds());

						this.currentTime = `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`;

					}, 1000);
				}
			},
			addZero(i) {
				if (i < 10) {
					i = "0" + i;
				}
				return i;
			},
			getglod_price() {
				let url = app.globalData.get_request_url("mxjjs");
				uni.request({
					url: url,
					method: "POST",
					data: {},
					success: (res) => {
						if(res.data.code == 200){
							var data = res.data.data
							this.glod_price = data.items;
							let code = "JZJ_au";
							this.glod_price = this.glod_price.find((item) => {
								return item.code === code;
							});
							this.glod_price.askprice = (this.glod_price.askprice*1).toFixed(2);
							this.glod_price.bidprice = (this.glod_price.bidprice*1).toFixed(2);
						}
					}
				})
			},
			// 提交
			submit_envent() {
				uni.showLoading({
					title: '处理中...'
				});
				var askprice = this.glod_price.askprice;
				var bidprice = this.glod_price.bidprice;
				var url = app.globalData.data.request_admin_url + 'metal/localPreciousMetal';
				var post_data = {
					tenantId:app.globalData.data.tenantId,
					code:'JZJ_au',
					askprice:askprice,
					bidprice:bidprice
				}
				uni.request({
					url: url,
					method: "POST",
					data:post_data,
					header: {
						'Authorization': 'Bearer ' + app.globalData.data.admin_token
					},
					success: (res) => {
						uni.hideLoading();
						if(res.data.code == 200){
							uni.showToast({
								title:res.data.msg
							})
							setTimeout(function() {
								uni.switchTab({
									url:"/pages/index/index"
								})
							}, 2000);
						}else {
							if (app.globalData.is_login_check(res.data)) {
								uni.showToast({
									title:res.data.msg
								})
							} else {
								uni.showToast({
									title:'提交失败，请重试！'
								})
							}
						}
					},
					fail: (err) => {
						uni.hideLoading();
						uni.showToast({
							title:"服务器请求出错!",
							icon:'error'
						})
					}
				})
			},
			OnEntryTime() {
				var radarlog_data = {
					pageUrl: "pages/gold_modify/gold_modify",
					name: "金价修改页",
					remark: "金价修改页停留时间",
					entryTime: this.entryTime,
				}
				uni.request({
					url: app.globalData.get_request_url("radarlog/add"),
					data: radarlog_data,
					method: "POST",
					success: (res) => {
						console.log(res, "金价修改页停留时间");
					}
				})
			}
		},
		onShareAppMessage(res) {
			return {
				title: app.globalData.data.tenantName,
				path: "pages/gold_modify/gold_modify?numberId=" + app.globalData.data.numberId,
				imageUrl: this.imgUrl,
				mpId: app.globalData.data.appid
			}
		},
	}
</script>

<style lang="less">
	page {
		background-color: #f2f2f2;
	}

	.gold_modify {
		width: 100%;
		height: 320rpx;
		background-color: #cca57e;

		.gold_info {
			display: flex;
			align-items: center;
			justify-content: center;
			padding-top: 20rpx;
			padding-bottom: 20rpx;
			color: white;

			.gold_time {
				padding-left: 10rpx;
			}
		}

		.gold_content {
			background-color: white;
			width: 90%;
			height: 200rpx;
			border-radius: 20rpx;
			margin: auto;
			display: flex;
			align-items: center;
			justify-content: space-around;

			.gold_content_info {
				.gold_content_name {
					font-size: 13px;
				}

				.gold_content_price {
					display: flex;
					align-items: center;

					input {
						width: 115rpx;
						border-bottom: 1px solid #f2f2f2;
						color: red;
					}

					.gold_content_dw {
						font-size: 13px;
					}
				}
			}
		}

		.gold_modify_btn {
			position: fixed;
			bottom: 50rpx;
			left: 10%;
			width: 80%;
			height: 70rpx;
			text-align: center;
			line-height: 70rpx;
			background-color: #cca57e;
			color: white;
			border-radius: 40rpx;
			;
		}
	}
</style>